package orest.back.membermanage;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;

import net.ssta.common.Constants;
import net.ssta.common.DataBaseConfig;
import net.ssta.core.base.BaseException;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.DataBaseExecutor;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.core.util.DateUtility;
import net.ssta.core.util.UUIDGenerator;
import net.ssta.util.bill.UserPoint;
import net.ssta.util.mail.MailConfig;
import net.ssta.util.mail.MailSender;
import net.ssta.util.mail.MailSenderInfo;
import orest.front.market.MarketHandler;

import org.apache.log4j.Logger;

import com.rc.sdk.crypt.CryptFactory;


public class RegisterUserHandler {
	private static Logger log = Logger.getLogger(RegisterUserHandler.class.getClass()); 
	
	
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public RecordSet userInfoList(Record searchData,PagingInfo pageingInfo) throws SQLException {
		RecordSet rs=null;
		Connection conn = null;
    	try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	        String sql=UserManagerSql.userInfoList(searchData, pageingInfo);
	    	rs=dbe.find(sql,pageingInfo);
	    	
		} catch (SQLException e) {
			log.error("展现用户信息列表出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
    	return rs;
	}
	
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public Record getUser(Record searchData) throws SQLException {
		Record user=new Record();
		Connection conn = null;
    	try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
    		String userName=searchData.getString("USERNAME","");
    		String EMAIL=searchData.getString("EMAIL","");
    		user.setConnection(conn);
    		user.setEntityName("V_REGISTER_USER");
    		user.addData("LOGINNAME",userName);
    		user.addData("EMAIL",EMAIL);
    		user.addPrimaryKey("EMAIL");
    		user.addPrimaryKey("LOGINNAME");
    		
    		user=user.findByPrimaryKey();
		} catch (SQLException e) {
			log.error("根据用户名和email查找用户出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
    	return user;
	}
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public RecordSet costRecordList(Record searchData,PagingInfo pageingInfo) throws SQLException {
		RecordSet rs=null;
		Connection conn = null;
    	try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	        String sql=UserManagerSql.costRecordList(searchData, pageingInfo);
	    	rs=dbe.find(sql,pageingInfo);
	    	
		} catch (SQLException e) {
			log.error("展现用户信息列表出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
    	return rs;
	}
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public RecordSet reChargeList(Record searchData,PagingInfo pageingInfo) throws SQLException {
		RecordSet rs=null;
		Connection conn = null;
    	try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	        String sql=UserManagerSql.reChargeList(searchData, pageingInfo);
	    	rs=dbe.find(sql,pageingInfo);
	    	
		} catch (SQLException e) {
			log.error("展现用户充值列表出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
    	return rs;
	}
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public RecordSet registerList(Record searchData,PagingInfo pageingInfo) throws SQLException {
		RecordSet rs=null;
		Connection conn = null;
    	try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	    	StringBuffer sql=new StringBuffer();
	    	sql.append("SELECT * ")
	    	   .append(" FROM V_REGISTER_USER WHERE  NEITYPE='1'");
	    	String loginName=searchData.getString("LOGINNAME","");
	    	String userName=searchData.getString("USERNAME","");
	    	String workOrg=searchData.getString("WORKORG","");
	    	String billRange=searchData.getString("BILLRANGE","");
	    	String vipRange=searchData.getString("VIPRANGE","");
	    	if(!"".equals(loginName)){
	    		sql.append(" AND loginName like '%").append(loginName).append("%' ");
	    	}
	    	if(!"".equals(userName)){
	    		sql.append(" AND FULLNAME like '%").append(userName).append("%' ");
	    	}
	    	if(!"".equals(workOrg)){
	    		sql.append(" AND WORKORG like '%").append(workOrg).append("%' ");
	    	}
	    	if(!"".equals(billRange)){
	    		 if("0".equals(billRange)){
	    			 sql.append(" AND (USERPOINT is null or USERPOINT=0) ");
	    		 }else  if("1".equals(billRange)){
	    			 sql.append(" AND USERPOINT between 1 and 100  ");
	    		 }else if("2".equals(billRange)){
	    			 sql.append(" AND USERPOINT between 101 and 1000 ");
	    		 }else if("3".equals(billRange)){
	    			 sql.append(" AND USERPOINT>1000 ");
	    		 }
	    	}
	    	if(!"".equals(vipRange)){
	    		String startDate=DateUtility.getString(new Date(), "yyyy-MM-dd");
	    		String enddate=null;
	    		 if("0".equals(vipRange)){ 
	    			 sql.append(" AND ENDDATE< '").append(startDate).append("'");
	    		 }else if("1".equals(vipRange)){
	    			 enddate=MarketHandler.getCalculateDate(0,0,7);
	    		 }else if("2".equals(vipRange)){
	    			 enddate=MarketHandler.getCalculateDate(0,1,0);
	    		 }else if("3".equals(vipRange)){
	    			 enddate=MarketHandler.getCalculateDate(0,6,0);
	    		 }else if("4".equals(vipRange)){
	    			 enddate=MarketHandler.getCalculateDate(1,0,0);
	    		 }
	    		 if(enddate!=null){
	    			 sql.append(" AND ENDDATE>= '").append(startDate).append("'");
	    			 sql.append(" AND ENDDATE<= '").append(enddate).append("'");
	    		 }
	    	}
	    	
	    	
	    	
	    	rs=dbe.find(sql.toString(),pageingInfo);
	    	
		} catch (SQLException e) {
			log.error("展现用户列表出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
    	return rs;
	}
	
	
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public RecordSet orderList(Record searchData,PagingInfo pageingInfo) throws SQLException {
		RecordSet rs=null;
		Connection conn = null;
    	try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	        String sql=UserManagerSql.orderList(searchData, pageingInfo);
	    	rs=dbe.find(sql,pageingInfo);
	    	
		} catch (SQLException e) {
			log.error("展现用户充值列表出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
    	return rs;
	}
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public void recharge(String userId,int money,String operatorId,String ipAddress,String mark) throws SQLException ,Exception{
    	try {
    		
    		UserPoint up=new UserPoint(userId);
    		up.recharge(money,operatorId,ipAddress,mark);
		} catch (SQLException e) {
			log.error("充值出错："+e);
			throw e;
		}catch (Exception e) {
			log.error("充值出错："+e);
			throw e;
		}
	}
	
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public void chargeBack(String userId,int money,String operatorId,String ipAddress,String mark) throws SQLException ,Exception{
    	try {
    		
    		UserPoint up=new UserPoint(userId);
    		up.chargeBack(money,operatorId,ipAddress,mark);
		} catch (SQLException e) {
			log.error("扣费出错："+e);
			throw e;
		}catch (Exception e) {
			log.error("扣费出错："+e);
			throw e;
		}
	}
	
	
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public void vipConfig(String userId,Date startDate,Date endDate,String operatorId,String ipAddress,int minusPoint) throws SQLException ,Exception{
    	try {
    		UserPoint up=new UserPoint(userId);
    		up.vipConfig(startDate, endDate, operatorId,ipAddress,minusPoint);
    		 
		} catch (SQLException e) {
			log.error("统包设置出错："+e);
			throw e;
		}catch (Exception e) {
			log.error("统包设置出错："+e);
			throw e;
		}
	}
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public static Record getUserById(String userId) throws SQLException ,Exception{
		Record r=null;
		Connection conn = null;
    	try {
    		if(userId==null||"".equals(userId))return null;
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
    		Record user=new Record();
    		user.setConnection(conn);
    		
    		user.setEntityName("V_REGISTER_USER");
    		user.addData("USERID",userId);
    		user.setPrimaryKey("USERID");
    		
    		r=user.findByPrimaryKey();
		} catch (SQLException e) {
			log.error("展现用户列表出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
    	return r;
	}
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public void addUser(Record r,Connection conn) throws SQLException {
		boolean isConn=true;
		if(conn==null){
			isConn=false;
		}
    	try {
    		String pwd=CryptFactory.createInstance().encrypt("",r.getString("PWD",""));//用户密码
    		String userId=UUIDGenerator.getUUID();//用户主键
    		
    		if(!isConn){ 
	    		conn=DataBaseConfig.getFrameworkDataSource().getConnection();
	    		conn.setAutoCommit(false);
    		}
    		Record user=new Record();
    		user.setPrimaryKey("USERID");
    		user.setConnection(conn);
    		user.addData("USERID",userId);
    		user.setEntityName("T_USER");
    		user.addData("USERID",userId);
    		user.addData("FULLNAME",r.getString("FULLNAME",""));
    		user.addData("ORGID",Constants.registerOrgId);
    		user.addData("SEX",r.getString("SEX",""));
    		user.addData("EMAIL",r.getString("EMAIL",""));
    		user.addData("TELEPHONE",r.getString("TELEPHONE",""));
    		user.addData("BIRTHDAY",r.getDate("BIRTHDAY"));
    		user.addData("WORKORG",r.getString("WORKORG",""));
    		user.addData("INDUSTRY",r.getString("INDUSTRY",""));
    		user.addData("PROVINCE",r.getString("PROVINCE",""));
    		user.addData("MEMBERTYPE",r.getString("MEMBERTYPE",""));
    		user.addData("VERIFYID",r.getString("VERIFYID",""));
    		user.create();
    		
    		
    		Record account=new Record();
    		account.setPrimaryKey("USERID");
    		account.setConnection(conn);
    		account.addData("USERID",userId);
    		account.setEntityName("T_USERACCOUNT");
    		account.addData("USERID",userId);
    		account.addData("USERNAME",r.getString("FULLNAME",""));
    		account.addData("LOGINNAME",r.getString("USERNAME",""));
    		account.addData("LOGINPWD",pwd);
    		account.addData("EMAIL",r.getString("EMAIL",""));
    		account.addData("DELTAG",r.getString("DELTAG",""));
    		account.addData("CREATETIME",new Date());
    		
    		account.create();
    		
		    Record userOrg=new Record();
		    userOrg.setPrimaryKey("UserOrgId");
		    userOrg.setConnection(conn);
		    userOrg.setEntityName("T_USERORG");
		    userOrg.addData("USERID",userId);
		    userOrg.addData("ISCURRENT","1");
		    userOrg.addData("ORGID",Constants.registerOrgId);
		    userOrg.create();
		    
		    if(!isConn){ 
		    	conn.commit();
		    }
    	} catch (SQLException e) {
			log.error("后台增加新用户出错："+e);
			throw e;
		}catch (Exception e) {
			log.error("后台增加新用户出错："+e);
		}finally{
			try{
				if(!isConn){ 
					if(conn != null) conn.close();
				}
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
	}
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public void addUser(Record r) throws SQLException {
		 addUser(r,null);
	}
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public void modUser(Record r) throws SQLException ,Exception{
		Connection conn=null;
    	try {
    	 
    	
    		String userId=r.getString("USERID");
    		
    		
    		conn=DataBaseConfig.getFrameworkDataSource().getConnection();
    		//conn.setAutoCommit(false);
    		
    		
    		Record user=new Record();
    		user.setPrimaryKey("USERID");
    		user.setConnection(conn);
    		user.addData("USERID",userId);
    		user.setEntityName("T_USER");
    		user.addData("FULLNAME",r.getString("FULLNAME",""));
    		user.addData("SEX",r.getString("SEX",""));
    		user.addData("EMAIL",r.getString("EMAIL",""));
    		user.addData("TELEPHONE",r.getString("TELEPHONE",""));
    		user.addData("BIRTHDAY",r.getDate("BIRTHDAY"));
    		
    		user.addData("WORKORG",r.getString("WORKORG",""));
    		user.addData("INDUSTRY",r.getString("INDUSTRY",""));
    		user.addData("PROVINCE",r.getString("PROVINCE",""));
    		user.addData("MEMBERTYPE",r.getString("MEMBERTYPE",""));
    		user.update();
    		
    		
    		Record account=new Record();
    		account.setConnection(conn);
    		account.setEntityName("T_USERACCOUNT");
    		account.addData("USERID",userId);
    		account.addData("EMAIL",r.getString("EMAIL",""));
    		account.setPrimaryKey("USERID");
    		account.store();
		    
		 //   conn.commit();
		    
    	} catch (SQLException e) {
			log.error("后台修改用户出错："+e);
			throw e;
		}catch (Exception e) {
			log.error("后台修改用户出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
	}
	
	/**
	 *  激活
	 * @return
	 * @throws SQLException
	 */
	public Record getUsrByverifyId(String verifyId) throws SQLException ,BaseException,Exception{
		Connection conn=null;
		Record r=new Record();
		try{
			conn=DataBaseConfig.getFrameworkDataSource().getConnection();
			
			r.setConnection(conn);
			r.addData("VERIFYID",verifyId);
			r.setPrimaryKey("VERIFYID");
			r.setEntityName("T_USER");
			r=r.findByPrimaryKey();
			if(r==null){
				throw new BaseException("验证码不存在",new Exception());
			}
		 
		} catch (SQLException e) {
			log.error("用户申请重置密码出错："+e);
			throw e;
		}catch (Exception e) {
			log.error("用户申请重置密码出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
		return r;
	}
	
	
	
	
	/**
	 *  激活
	 * @return
	 * @throws SQLException
	 */
	public void clearVerifyId(String userId) throws SQLException ,BaseException,Exception{
		Connection conn=null;
		try{
			conn=DataBaseConfig.getFrameworkDataSource().getConnection();
			Record r=new Record();
			r.setConnection(conn);
			r.addData("USERID",userId);
			r.setPrimaryKey("USERID");
			r.addData("VERIFYID","");
			r.setEntityName("T_USER");
			 
		    r.update();
		    
		} catch (SQLException e) {
			log.error("用户激活出错："+e);
			throw e;
		}catch (Exception e) {
			log.error("用户激活出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
	}
	
	/**
	 *  激活
	 * @return
	 * @throws SQLException
	 */
	public void verify(String verifyId) throws SQLException ,BaseException,Exception{
		Connection conn=null;
		try{
			conn=DataBaseConfig.getFrameworkDataSource().getConnection();
			conn.setAutoCommit(false);
			Record r=new Record();
			r.setConnection(conn);
			r.addData("VERIFYID",verifyId);
			r.setPrimaryKey("VERIFYID");
			r.setEntityName("T_USER");
			r=r.findByPrimaryKey();
			if(r==null){
				throw new BaseException("验证码不存在",new Exception());
			}
			
			String userId=r.getString("USERID","");
		    Record ua=new Record();
		    ua.setConnection(conn);
		    ua.setPrimaryKey("USERID");
		    ua.setEntityName("T_USERACCOUNT");
		    ua.addData("DELTAG", "false");
		    ua.addData("USERID",userId);
		    ua.update();
		    
		    Record user=new Record();
		    user.setConnection(conn);
		    user.setPrimaryKey("USERID");
		    user.setEntityName("T_USER");
		    user.addData("USERID",userId);
		    user.addData("VERIFYID","");
		    user.update();
		    
		    conn.commit();
		} catch (SQLException e) {
			log.error("用户激活出错："+e);
			throw e;
		}catch (Exception e) {
			log.error("用户激活出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
	}

	/**
	 *  注册
	 * @return
	 * @throws SQLException
	 * @throws MessagingException 
	 * @throws UnsupportedEncodingException 
	 */
	public void register(Record r,HttpServletRequest request) throws SQLException, UnsupportedEncodingException, MessagingException{
		Connection conn=null;
		try{
			conn=DataBaseConfig.getFrameworkDataSource().getConnection();
			String verifyId=UUIDGenerator.getUUID();//注册时的验证码
			r.addData("VERIFYID",verifyId);
			r.addData("DELTAG","true");
			this.addUser(r);
			String serverAddress=request.getServerName();
			if(!"80".equals(request.getServerPort())){
				serverAddress+=":"+request.getServerPort();
			}
			String userName=r.getString("USERNAME","");
			this.sendMail(r.getString("EMAIL",""),verifyId,"register",serverAddress,userName);
		} catch (SQLException e) {
			log.error("后台修改用户出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
	}
	
	
	

	/**
	 *  忘记密码
	 * @return
	 * @throws SQLException
	 * @throws MessagingException 
	 * @throws UnsupportedEncodingException 
	 */
	public void getPwd(String userId,String email,HttpServletRequest request,String userName) throws SQLException, UnsupportedEncodingException, MessagingException {
		Connection conn=null;
		try{
			String verifyId=UUIDGenerator.getUUID();//注册时的验证码
			conn=DataBaseConfig.getFrameworkDataSource().getConnection();
			conn.setAutoCommit(false);
			Record r=new Record();
			r.setConnection(conn);
			r.addData("VERIFYID",verifyId);
			r.setPrimaryKey("USERID");
			r.addData("USERID",userId);
			r.setEntityName("T_USER");
			r.update();
			String serverAddress=request.getServerName();
			if(!"80".equals(request.getServerPort())){
				serverAddress+=":"+request.getServerPort();
			}
			this.sendMail(email,verifyId,"pwd",serverAddress,userName);
		   conn.commit();
		    
		} catch (SQLException e) {
			log.error("用户取回密码出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
	}
	
	/**
	 * 发送邮件
	 * @param email
	 * @param verifyId
	 * @throws MessagingException 
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	public void sendMail(String email,String verifyId,String type,String serverAddress,String userName) throws UnsupportedEncodingException, MessagingException  {
		MailSenderInfo mailInfo=MailConfig.getMailSenderInfo();
		String context=null;
		if("pwd".equals(type)){
			mailInfo.setSubject("安科网用户申请重置密码");
			context=this.getTemplateString("mailPwdTemplate.txt");
		}else{
			mailInfo.setSubject("欢迎您注册安科网，您需要进行验证以完成您的注册");
			context=this.getTemplateString("mailTemplate.txt");
		}
		context=context.replaceAll("verifyIdVAL", verifyId);
		context=context.replaceAll("serverAddress", serverAddress);
		context=context.replaceAll("userName", userName);
		 mailInfo.setContent(context);
		 mailInfo.setToAddress(new String[] {email});
		 MailSender.sendHtmlMailInfo(mailInfo);
		//System.out.println(context);
		   
	}
	
	private String getTemplateString(String fileName){
		String fileContent=null;
		try{
			ClassLoader ccl = Thread.currentThread().getContextClassLoader();
			InputStream fis = ccl.getResourceAsStream(fileName);
			StringBuffer  buffer = new StringBuffer();
			   BufferedReader in = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
			   String line = ""; 
			   while ((line = in.readLine()) != null){
			     buffer.append(line);
			   }
			fileContent = buffer.toString();
		 
		}catch(Exception e){
			log.error(e);
		}
		return fileContent;
	}
	
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public Record getUserUpgradeInfo(String userId) throws SQLException ,Exception{
		Connection conn=null;
		Record r=new Record();
		try{
			conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
			
			r.setConnection(conn);
			r.addData("USERID",userId);
			r.setPrimaryKey("USERID");
			r.setEntityName("T_USERUPGRADE");
			r=r.findByPrimaryKey();
			if(r==null){
				return null;
			}
			
			String fkId=r.getString("FKID","");
			String fkType=r.getString("FKTYPE");
			if("0".equals(fkType)&&!"".equals(fkId)){
				Record unit=getUnitById(fkId);
				r.addData("FK",unit);
			}if("1".equals(fkType)&&!"".equals(fkId)){
				Record expert=getExpertById(fkId);
				r.addData("FK",expert);
			}
			
		 
		} catch (SQLException e) {
			log.error("用户得到升级信息出错："+e);
			throw e;
		}catch (Exception e) {
			log.error("用户得到升级信息出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
		return r;
	}
	
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public Record getExpertById(String expertId) throws SQLException ,Exception{
		Connection conn=null;
		Record r=new Record();
		try{
			conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
			
			r.setConnection(conn);
			r.addData("EXPERTID",expertId);
			r.setPrimaryKey("EXPERTID");
			r.setEntityName("T_EXPERT");
			r=r.findByPrimaryKey();
			if(r==null){
				return null;
			}
		} catch (SQLException e) {
			log.error("根据ID找机构出错："+e);
			throw e;
		}catch (Exception e) {
			log.error("根据ID找机构出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
		return r;
	}
	
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public Record getUnitById(String unitId) throws SQLException ,Exception{
		Connection conn=null;
		Record r=new Record();
		try{
			conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
			r.setConnection(conn);
			r.addData("UNITID",unitId);
			r.setPrimaryKey("UNITID");
			r.setEntityName("T_UNIT");
			r=r.findByPrimaryKey();
			if(r==null){
				return null;
			}
		} catch (SQLException e) {
			log.error("根据ID找机构出错："+e);
			throw e;
		}catch (Exception e) {
			log.error("根据ID找机构出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
		return r;
	}
	
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public Record applyUpgrade(Record r) throws SQLException ,Exception{
		Connection conn=null;
	
		try{
			conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
			r.setConnection(conn);
			r.setPrimaryKey("USERID");
			r.setEntityName("T_USERUPGRADE");
			r.addData("APPTIME",new Date());
			r.create();
		} catch (SQLException e) {
			log.error("提交申请出错："+e);
			throw e;
		}catch (Exception e) {
			log.error("提交申请出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
		return r;
	}
	
	
	
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public RecordSet applyList(Record searchData,PagingInfo pageingInfo) throws SQLException {
		RecordSet rs=null;
		Connection conn = null;
    	try {
    		
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
	    	DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
	        String sql=UserManagerSql.applyList(searchData);
	    	rs=dbe.find(sql,pageingInfo);
	    	
		} catch (SQLException e) {
			log.error("展现用户升级申请列表出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
    	return rs;
	}
	
	
	 
	
	/**
	 *  
	 * @return
	 * @throws SQLException
	 */
	public Record setUnitUser(String userId,String unitId) throws SQLException ,Exception{
		Connection conn=null;
		Record r=new Record();
		try{
			conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
			r.setConnection(conn);
			r.setPrimaryKey("USERID");
			r.setEntityName("T_USERUPGRADE");
			r.addData("USERID",userId);
			r.addData("FKID",unitId);
			r.update();
		} catch (SQLException e) {
			log.error("提交申请出错："+e);
			throw e;
		}catch (Exception e) {
			log.error("提交申请出错："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
		return r;
	}
	
	/**
	 *   修改用户的删除标记
	 * @return
	 * @throws SQLException
	 */
	public void updateUserDel(String userId,boolean deltag) throws SQLException ,BaseException,Exception{
		Connection conn=null;
		try{
			conn=DataBaseConfig.getFrameworkDataSource().getConnection();
			Record r=new Record();
			r.setConnection(conn);
			r.addData("USERID",userId);
			r.setPrimaryKey("USERID");
			r.addData("DELTAG",deltag);
			r.setEntityName("T_UserAccount");
		    r.update();
		    
		} catch (SQLException e) {
			log.error("修改用户的删除标记："+e);
			throw e;
		}catch (Exception e) {
			log.error("修改用户的删除标记："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
	}
	
	
	
	
	/**
	 *   修改用户的删除标记
	 * @return
	 * @throws SQLException
	 */
	public void ipConfig(String userId,String[] ip,String[] ipend) throws SQLException ,BaseException,Exception{
		Connection conn=null;
		try{
			conn=DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
			conn.setAutoCommit(false);
			Record r=new Record();
			r.setConnection(conn);
			r.addData("USERID",userId);
			r.setPrimaryKey("USERID");
			r.setEntityName("T_USER_IPCONFIG");
		    r.remove();
		    
		    
		    for(int i=0;i<ip.length;i++){
		    	if(ip[i]==null||"".equals(ip[i])){
		    		continue;
		    	}
		    	Record insert=new Record();
		    	insert.setConnection(conn);
		    	insert.addData("USERID",userId);
		    	insert.addData("IP",ip[i]);
		    	insert.addData("IPEND",ipend[i]);
		    	insert.setEntityName("T_USER_IPCONFIG");
		    	insert.create();
		    }
		    
		    conn.commit();
		} catch (SQLException e) {
			log.error("修改用户的IP设置："+e);
			throw e;
		}catch (Exception e) {
			log.error("修改用户的IP设置："+e);
			throw e;
		}finally{
			try{
				if(conn != null) conn.close();
			}catch (SQLException e){
				 log.error("关闭conn出错",e);
			}
		}
	}
	
	
	
	
	public static void main(String[] args)throws Exception{
		RegisterUserHandler handler=new RegisterUserHandler();
		//String a=handler.getTemplateString("mailTemplate.txt");
		
	     handler.sendMail("email", "1234verifyId", "type", "192.168.1.122:8080","fdy12345");
		//String a=CryptFactory.createInstance().encrypt("","1");//用户密码
		//System.out.println(a);
	}
		
}
